package com.lw.carmanage.service.impl;

import com.lw.carmanage.cache.CarCache;
import com.lw.carmanage.exceptions.MyErrorCode;
import com.lw.carmanage.exceptions.QueryBaseException;
import com.lw.carmanage.mapper.CarMapper;
import com.lw.carmanage.pojo.Car;
import com.lw.carmanage.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/4/4 18:50
 */
@Service
public class CarServiceImpl implements CarService {

    private CarCache carCache;

    @Autowired
    public void setCarCache(CarCache carCache) {
        this.carCache = carCache;
    }

    private CarMapper carMapper;

    @Autowired
    public void setCarMapper(CarMapper carMapper) {
        this.carMapper = carMapper;
    }

    @Override
    public void addCar(Car car) {
        Car byName = carMapper.findByName(car.getName());
        Assert.isNull(byName,()->{
            throw new QueryBaseException("品牌已存在", MyErrorCode.DATA_ALREADY_EXIST);
        });
        carMapper.addCar(car);
    }

    @Override
    public Car findById(Long id) {
        return carCache.get(id);
    }
}
